﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ServiceModel.Web;
using Microsoft.Http;
using System.IO;
using System.Xml.Serialization;
using System.ServiceModel;
using CoderJoe.WoW.Services;
using System.Drawing;
using System.Xml.Linq;
using CoderJoe.Xml;

namespace CoderJoe.WoW.Proxies.Armory
{
	public class WoWArmoryProxy
		: WoWArmoryProxyBase, IWoWArmory
	{
		public Character GetCharacter(string realm, string name)
		{
			Character character = new Character();
			GetCharacterSheetResult.page characterSheet = GetCharacterSheet<GetCharacterSheetResult.page>(realm, name);

			character.BattleGroup = characterSheet.characterInfo.character.battleGroup;
			character.CharacterClass = characterSheet.characterInfo.character.@class;
			character.Name = characterSheet.characterInfo.character.name;
			character.Realm = characterSheet.characterInfo.character.realm;

			foreach (var remoteItem in characterSheet.characterInfo.characterTab.items)
			{
                if (remoteItem.slot >= 0)
                {
                    CharacterItem item = new CharacterItem();

                    item.CurrentDurability = remoteItem.durability;
                    item.Durability = remoteItem.maxDurability;
                    item.EnchantId = remoteItem.permanentenchant;
                    item.Gems[0] = remoteItem.gem0Id;
                    item.Gems[1] = remoteItem.gem1Id;
                    item.Gems[2] = remoteItem.gem2Id;
                    item.Icon = remoteItem.icon;
                    item.ItemId = remoteItem.id;
                    item.SlotIndex = remoteItem.slot;

                    character.Items[item.SlotIndex] = item;
                }
			}

			return character;
		}

		public Item GetItem(int itemId)
		{
			Item item = new Item();
			var remoteItem = GetItemInfo<GetItemInfoResult.page>(itemId);

			item.Icon = remoteItem.itemInfo.item.icon;
			item.ItemId = remoteItem.itemInfo.item.id;
			item.Level = remoteItem.itemInfo.item.level;
			item.Name = remoteItem.itemInfo.item.icon;
			item.Quality = remoteItem.itemInfo.item.quality;
			item.Type = remoteItem.itemInfo.item.type;

			return item;
		}

        public ItemDetail GetItemDetail(int itemId)
        {
            ItemDetail item = new ItemDetail();
            var remoteItem = GetItemDetailInfo<XElement>(itemId);

            item.ItemId = remoteItem.GetValue<int>("itemTooltips/itemTooltip/id");
            item.Armor = remoteItem.GetValue<int>("itemTooltips/itemTooltip/armor");
            item.BonusArmor = remoteItem.GetValue<int>("itemTooltips/itemTooltip/armor/armorBonus");

            item.BonusIntellect = remoteItem.GetValue<int>("itemTooltips/itemTooltip/bonusIntellect");
            item.BonusHitRating = remoteItem.GetValue<int>("itemTooltips/itemTooltip.bonusHitRating");
            item.InventoryTypeId = remoteItem.GetValue<int>("itemTooltips/itemTooltip/equipData.inventoryType");
            item.SubClassName = remoteItem.GetValue<string>("itemTooltips/itemTooltip/equipData.subclassName");
            item.Bonding = remoteItem.GetValue<int>("itemTooltips/itemTooltip/bonding");
            item.Name = remoteItem.GetValue<string>("itemTooltips/itemTooltip/name");
            item.QualityId = remoteItem.GetValue<int>("itemTooltips/itemTooltip/overallQualityId");
            item.ClassId = remoteItem.GetValue<int>("itemTooltips/itemTooltip/classId");
            item.BonusStamina = remoteItem.GetValue<int>("itemTooltips/itemTooltip/bonusStamina");
            item.BonusSpellPower = remoteItem.GetValue<int>("itemTooltips/itemTooltip/bonusSpellPower");
            item.BonusResilienceRating = remoteItem.GetValue<int>("itemTooltips/itemTooltip/bonusResilienceRating");

            //item.AllowedClasses = remoteItem.GetValue<int>("itemTooltips.itemTooltip.allowableClasses");

            return item;
        }

		public Bitmap GetItemImage(string imageName)
		{
			using (Stream buffer = GetItemImage<Stream>(imageName))
			{
				return (Bitmap)Bitmap.FromStream(buffer);
			}
		}

		#region IWoWArmory Members

		public TResult GetCharacterSheet<TResult>(string realm, string name)
		{
            return ExecuteMethod<TResult>(
				System.Reflection.MethodInfo.GetCurrentMethod(),
				realm, name);
		}

        public TResult GetItemInfo<TResult>(int itemId)
		{
            return ExecuteMethod<TResult>(
				System.Reflection.MethodInfo.GetCurrentMethod(),
				itemId);
		}

        public TResult GetItemImage<TResult>(string imageName)
		{
            return ExecuteMethod<TResult>(
				System.Reflection.MethodInfo.GetCurrentMethod(),
				imageName);
		}

        public TResult GetItemDetailInfo<TResult>(int itemId)
        {
            return ExecuteMethod<TResult>(
                System.Reflection.MethodInfo.GetCurrentMethod(),
                itemId);
        }

        #endregion
    }
}
